package com.ruoyi.web.controller.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PrometheusAPIUtils;
import com.ruoyi.system.domain.ComposeCompare;
import com.ruoyi.system.domain.ComposeCompareQuery;
import com.ruoyi.system.domain.SYApiData;
import com.ruoyi.system.domain.SYApiDataChild;
import com.ruoyi.system.domain.SYApiResult;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysOperLogService;
import com.ruoyi.test.Test;


/**
 * 页面跳转处理
 * 
 * @author ljw
 */
@Controller
@RequestMapping("/data")
public class DataController extends BaseController{
    @Autowired
    private ISysConfigService configService;
    
    private static String protoType3;
    
    private static String protoType2;
    
    private static final String TOKEN_KEY = "62b200b08aec46afbea77580cb0748b0";
	 
    /**
     * 协议数据管理-字典配置
     */
    @GetMapping("/dictConfig")
    public String dictConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
    	
   	 	mmap.put("manageIp", manageIp);
        return "data_service/dictionary_config";
    }
    
    /**
     * 协议数据管理-字段解析表配置
     */
    @GetMapping("/fieldAnalysis")
    public String fieldAnalysis(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
   	    String manageIp = request.getServerName();
   	 	
   	 	mmap.put("manageIp", manageIp);
        return "data_service/parselist_config";
    }
    
    /**
     * 协议数据管理-字段配置
     */
    @GetMapping("/fieldConfig")
    public String fieldConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/proto_field_config";
    }
    
    /**
     * 协议数据管理-协议配置
     */
    @GetMapping("/protoConfig")
    public String protoConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/proto_config";
    }
    
    /**
     * 协议数据管理-协议规则配置
     */
    @GetMapping("/ruleConfig")
    public String ruleConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/rule_config";
    }
    
    /**
     * 协议数据管理-协议服务配置
     */
    @GetMapping("/protoServiceConfig")
    public String protoServiceConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/proto_service_config";
    }
    
    /**
     * 大数据配置管理-数据规则配置
     */
    @GetMapping("/bigdataRule")
    public String bigDataRule(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/bigdata_rule";
    }
    
    /**
     * 大数据配置管理-大数据服务配置
     */
    @GetMapping("/bigdataServiceConfig")
    public String bigdataServiceConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/bigdata_service_config";
    }
    
    /**
     * 业务数据管理维护-数据库信息展示
     */
    @GetMapping("/databseInformation")
    public String databseInformation(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
   	 	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/database_information_statistics";
    }
    
    /**
     * 业务数据管理维护-数据库数据管理(超级管理员)
     */
    @GetMapping("/databseDataManagementSuper")
    public String databseDataManagementSuper(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	String manageUrl = "http://"+manageIp+":8080/dem/?auto_login_type=1&version=2&dem_params=%7B%22dem_user%22:%22admin%22,%22dem_pwd%22:%22888888%22%7D&valid_time=%2023102700";
   	 	mmap.put("manageIp", manageIp);
   	 	mmap.put("manageUrl", manageUrl);
        return "data_service/database_data_management_super";
    }
    
    /**
     * 存储设备管理维护-存储设备管理
     */
    @GetMapping("/storageDeviceManagement")
    public String storageDeviceManagement(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
//   	 	String manageUrl = "http://"+manageIp+":8082/index-sy.html#/dashboard";
    		String manageUrl = "https://192.168.250.142:443/index.html#/dashboard";
   	 	mmap.put("manageIp", manageIp);
   	 	mmap.put("manageUrl", manageUrl);
        return "data_service/storage_device_management";
    }
    
    /**
     * 存储设备管理维护-数据库信息展示
     */
    @GetMapping("/databaseMessage")
    public String databaseMessage(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
//   	 	String manageUrl = "http://"+manageIp+":8082/index-sy.html#/dashboard";
    		String manageUrl = "http://"+manageIp+":3000/d/-4yAHU-Sz/shu-ju-ku-xin-xi?orgId=1&kiosk";
    		mmap.put("manageIp", manageIp);
   	 	mmap.put("manageUrl", manageUrl);
        return "data_service/database_message";
    }
    
    /**
     * 业务数据管理维护-数据库数据管理(普通管理员)
     */
    @GetMapping("/databseDataManagementCommon")
    public String databseDataManagementCommon(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	
    	String manageIp = request.getServerName();
   	 	
    	//获取协议名称List
		JSONObject param = new JSONObject();
		param.put("pageSize", 50);//
		param.put("pageNo", 0);//
		//param.put("tokenKey", TOKEN_KEY);
		
		//String manageIp = "127.0.0.1";
   	 	
		Map<String,String> protoNameList = new HashMap<String,String>();
		
		//表名称对应关系
		Map<String,String> protoTablesMap = new LinkedHashMap();
		
		try {
			//System.out.println( "/dict/query请求的param：==" + param+ "==" );
			
			String result = PrometheusAPIUtils.payloadRequest("http://"+manageIp+":8081/dict/query", param);
			//String result = Test.protoDict;
			
			//System.out.println("一/dict/query请求的结果："+result);
			JSONObject jsonObj = JSONObject.parseObject(result);
			List<JSONObject> jsonList = (List<JSONObject>)JSONObject.parseObject(jsonObj.get("data").toString()).get("content");
			for(JSONObject jsonCObj : jsonList) {
				Integer cfgType = (Integer)jsonCObj.get("cfgType");
				if(cfgType == 4) {
					String content = jsonCObj.get("content").toString();
					List<JSONObject> protocolsList = (List<JSONObject>)JSONObject.parseObject(content).get("protocols");
					for(JSONObject protocolsObj : protocolsList) {
						String protoName = protocolsObj.get("name").toString();
						String segmentList = protocolsObj.get("segment_list").toString();
						protoNameList.put(protoName, segmentList);
					}
				}
				if(cfgType == 3) {
					protoType3 = jsonCObj.toString();
				}
				
				if(cfgType == 2) {
					protoType2 = jsonCObj.toString();
				}
				
				//查询数据库中文名称
				if(cfgType == 9) {
					String content = jsonCObj.get("content").toString();
					List<JSONObject> protocolsList = (List<JSONObject>)JSONObject.parseObject(content).get("tables");
					for(JSONObject protocolsObj : protocolsList) {
						String protoTableEn = protocolsObj.get("tlbname").toString();
						String protoTableZh = protocolsObj.get("protoname").toString();
						protoTablesMap.put(protoTableEn, protoTableZh);
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}		
		
		
		//表名称获取结果
		Map<String,String[]> protoTablesMapResult = new LinkedHashMap();
		/**
		 * 从数据库获取数据表信息
		 */
   	 	String resultTables = PrometheusAPIUtils.payloadRequest("http://"+manageIp+":8081/protoTable/queryAllMainTables", param);
		
   	 	JSONObject jsonObjTables = JSONObject.parseObject(resultTables);
   	 	try {
   	   	 	List<String> jsonListTablses = (List<String>)jsonObjTables.get("data");
   			
//   			List<String> jsonListTablses = new ArrayList<>();
//   			jsonListTablses.add("DATASET_ronghe");
   			
   	   	 	//List<String> mainTableList = new ArrayList<>();
   	   	 	for(String jsobjTable : jsonListTablses) {
   	   	 		//String[] splitStr = jsobjTable.split("_");
   	   	 		//if(splitStr.length == 2) {//主表
   	   	 			String realMainTableName = jsobjTable.replaceAll("dataset_", "").replaceAll("DATASET_", "");
   	   	 			String result = protoTablesMap.get(realMainTableName);
   	   	 			if(result != null && !"".equals(result)) {
   	   	 				
   	   	 				
   	   	 				//result
   	   	 				String hasFields = protoNameList.get(result);
   	   	 				String zhFields = result+"("+realMainTableName+")";
   	   	 				
   	   	 				String[] strPut = {zhFields,hasFields};
   	   	 				
   	   	 				protoTablesMapResult.put(jsobjTable, strPut);
   	   	 				//System.out.println("field:"+hasFields);
   	   	 			}else {
   	   	 				String[] putStr = {realMainTableName,""};
   	   	 				protoTablesMapResult.put(jsobjTable, putStr);
   	   	 			}
   	   	 			//mainTableList.add(jsobjTable);
   	   	 		//}
   	   	 	}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		
		
   	 	//获取字段名称List
		mmap.put("protoNameList", protoNameList);
		mmap.put("protoTablesMapResult", protoTablesMapResult);
   	 	mmap.put("manageIp", manageIp);
        return "data_service/database_data_management_common";
    }
    
    /**
     * 业务数据管理维护-数据库数据管理 获取数据列表
     */
    @PostMapping("/getProtoSegment")
    @ResponseBody
    public Map<String,String> getProtoSegment(String segmentList,HttpServletRequest request) {
    	Map<String,String> protoSegmentList = new LinkedHashMap<>();
    	if(segmentList != null && !"".equals(segmentList)) {
    		Object[] strArr = JSONObject.parseArray(segmentList).toArray();
    		
    		String content = JSONObject.parseObject(protoType3).get("content").toString();
//    		System.out.println("xixi:"+content);
			List<JSONObject> protocolsList = (List<JSONObject>)JSONObject.parseObject(content).get("protocol_segments");

//    		System.out.println("xixi:"+protocolsList.size());
        	for(Object objStr : strArr) {
    			for(JSONObject protocolsObj : protocolsList) {
    				String proto3Id = protocolsObj.get("id").toString();//字段ID
    				String proto3Name = protocolsObj.get("name").toString();//字段中文名称
    				
    				if(objStr.toString().equals(proto3Id)) {
    					String dbFiledName = protocolsObj.get("dbfield").toString();//字段英文名称
        				//String fieldUnit = protocolsObj.get("unit").toString();//字段单位
    					protoSegmentList.put(dbFiledName, proto3Name);
    				}
    			}
        	}
    	}
    	
    	return protoSegmentList;
    }
    
    /**
     * 业务数据管理维护-数据库数据管理 获取数据列表
     
    @PostMapping("/getProtoSegment")
    @ResponseBody
    public List<String> getProtoSegment(String segmentList,HttpServletRequest request) {
    	List<String> protoSegmentList = new ArrayList<>();
    	if(segmentList != null && !"".equals(segmentList)) {
    		Object[] strArr = JSONObject.parseArray(segmentList).toArray();
    		
    		String content = JSONObject.parseObject(protoType3).get("content").toString();
//    		System.out.println("xixi:"+content);
			List<JSONObject> protocolsList = (List<JSONObject>)JSONObject.parseObject(content).get("protocol_segments");

//    		System.out.println("xixi:"+protocolsList.size());
        	for(Object objStr : strArr) {
    			for(JSONObject protocolsObj : protocolsList) {
    				String proto3Id = protocolsObj.get("id").toString();//字段ID
    				String proto3Name = protocolsObj.get("name").toString();//字段中文名称
    				
    				if(objStr.toString().equals(proto3Id)) {
    					//String dbFiledName = protocolsObj.get("dbfield").toString();//字段英文名称
        				//String fieldUnit = protocolsObj.get("unit").toString();//字段单位
    					protoSegmentList.add(proto3Name);
    				}
    			}
        	}
    	}
    	
    	return protoSegmentList;
    }
    */
    
    /**
     * 业务数据管理维护-数据库数据管理 获取数据表头
     */
    @PostMapping("/getCustomField")
    @ResponseBody
    public Map<String,String> getCustomField(String segmentList,String tableName,HttpServletRequest request) {
    	//System.out.println(tableName);
    	Map<String,String> result = new LinkedHashMap<>();
    	
    	//设置管理字段
    	result.put("id", "数据ID");//a
    	result.put("inserttime", "插入时间");//a
    	result.put("updatetime", "更新时间");//a
    	result.put("customLabel", "自定义字段");//a
    	
    	if(segmentList != null && !"".equals(segmentList)) {
    		Object[] strArr = JSONObject.parseArray(segmentList).toArray();
    		
    		String content = JSONObject.parseObject(protoType3).get("content").toString();
			List<JSONObject> protocolsList = (List<JSONObject>)JSONObject.parseObject(content).get("protocol_segments");

        	for(Object objStr : strArr) {
    			for(JSONObject protocolsObj : protocolsList) {
    				String proto3Id = protocolsObj.get("id").toString();//字段ID
    				String proto3Name = protocolsObj.get("name").toString();//字段中文名称
    				
    				if(objStr.toString().equals(proto3Id)) {
    					String dbFiledName = protocolsObj.get("dbfield").toString();//字段英文名称
        				//String fieldUnit = protocolsObj.get("unit").toString();//字段单位
    					String value = dbFiledName + "(" + proto3Name + ")";
    					result.put(dbFiledName,value);
    				}
    			}
        	}
    	}
    	
//    	for(int i=1 ; i<80; i++) {			
//    		result.put("field_+"+i, "字段_+"+i);
//		}
    	
    	return result;
    }
    
    /**
     * 业务数据管理维护-数据库数据管理 获取数据二级查询表头
     */
    @PostMapping("/getCustomFieldSecond")
    @ResponseBody
    public Map<String,String> getCustomFieldSecond(String tableName,String fieldName,HttpServletRequest request) {
    	
    	
    	String manageIp = request.getServerName();
//    	System.out.println("ccc:"+fieldName);
    	Map<String,String> result = new LinkedHashMap<>();
    	
    	
    	String content = JSONObject.parseObject(protoType2).get("content").toString();
		List<JSONObject> protocolsList = (List<JSONObject>)JSONObject.parseObject(content).get("parse_list");		
		
		JSONObject toAnlaysObj = null;
    	int breakLabel = 0;
		for(JSONObject protocolsObj : protocolsList) {
			if(breakLabel != 0) {
				break;
			}
			//String proto3Id = protocolsObj.get("id").toString();//字段ID
			String proto2Name = protocolsObj.get("name").toString();//字段中文名称
			
			String fieldEnName = "";
			
			//查字段英文名称
			String content1 = JSONObject.parseObject(protoType3).get("content").toString();
			List<JSONObject> protocolsList1 = (List<JSONObject>)JSONObject.parseObject(content1).get("protocol_segments");
			for(JSONObject protocolsObj1 : protocolsList1) {
				String proto3Name = protocolsObj1.get("name").toString();//字段中文名称
				if(proto2Name.equals(proto3Name)) {
					System.out.println("bbb:"+protocolsObj.toJSONString());
					Object obj = protocolsObj1.get("dbfield");
					if(obj != null) {
						fieldEnName = (String)obj;//字段英文名称
					}
					//fieldEnName = protocolsObj.get("dbfield").toString();//字段英文名称
					if(fieldEnName.equals(fieldName)) {//如果能匹配到传递过来的英文字段名
						System.out.println("ccc:"+protocolsObj.toJSONString());
						toAnlaysObj = protocolsObj;
						breakLabel++;
						break;
					}
				}
			}
		}
		
		if(toAnlaysObj != null) {
			List<JSONObject> fieldList = (List<JSONObject>)toAnlaysObj.get("context");
			for(JSONObject jsf : fieldList) {
				String zhName = (String)jsf.get("name");//中文名称
				String enName = (String)jsf.get("dbfield");//英文名称
				result.put(enName, enName+"("+zhName+")");
			}
		}
    	
		
		/**
		 * 如果没存上
		 */
		if(result != null && result.isEmpty()) {
			//System.out.println("tn:"+tableName+"==fn:"+fieldName);
	    	String myTableName = tableName + "_" + fieldName;
			JSONObject param = new JSONObject();
			param.put("tableName", myTableName);//
	    	String fieldList = PrometheusAPIUtils.payloadRequest("http://"+manageIp+":8081/protoData/queryFieldByName", param);
	    	
	    	JSONObject jsonObjTables = JSONObject.parseObject(fieldList);
	   	 	List<String> jsonListFields = (List<String>)jsonObjTables.get("data");
	    	
	    	//设置管理字段
	    	//result.put("inserttime", "插入时间");//a
	    	
	    	for(String field : jsonListFields) {
	    		result.put(field, field.toLowerCase());
	    	}
		}
    	
    	return result;
    }
    
    /**
     * 业务数据管理维护-数据库数据管理 获取数据列表
     */
    @PostMapping("/databaseList")
    @ResponseBody
    public TableDataInfo databaseList(ComposeCompareQuery composeCompareQuery,HttpServletRequest request) {
    	
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
    	System.out.println( "一级查询条件composeCompareQuery：==" + composeCompareQuery+ "==" );
    	//List<SYApiData> syaList = new ArrayList<SYApiData>();
    	
    	startPage();
    	PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
    	System.err.println("pageNum:"+pageNum);
		JSONObject param = new JSONObject();
		param.put("pageSize", pageSize);//
		param.put("pageNo", pageNum-1);//
		param.put("tokenKey", TOKEN_KEY);
		
		if(composeCompareQuery != null) {
			String startTime = composeCompareQuery.getStartTime();
			String endTime = composeCompareQuery.getEndTime();
			String protoName = composeCompareQuery.getProtoName();
			List<ComposeCompare> ccList = composeCompareQuery.getComposeCompare();
			if(protoName != null && !"".equals(protoName)) {
				param.put("protoName", protoName);
			}
			if(ccList != null && !ccList.isEmpty()) {
				param.put("compareList", ccList);
			}
			if(startTime != null && !"".equals(startTime)) {
				param.put("startTime", startTime);//
			}
			if(endTime != null && !"".equals(endTime)) {
				param.put("endTime", endTime);//
			}
		}
		
		List<Map> resultList = new ArrayList<Map>();
		//{"protoKey":"山底气压15","valOfKey":"169"}
		//String result = PrometheusAPIUtils.getHttp("http://192.168.2.2:8081/protoData/query", param);
		long total = 0;
		try {
			System.out.println( "一级查询条件param：==" + param+ "==" );
			String result = PrometheusAPIUtils.payloadRequest("http://"+manageIp+":8081/protoData/query", param);
			//System.out.println("一级查询结果："+result);
			JSONObject jsonObj = JSONObject.parseObject(result);
			List<JSONObject> protocolsList = (List<JSONObject>)jsonObj.getJSONObject("data").get("content");
			
			
			for(JSONObject jsobj : protocolsList) {
				Map<String,Object> rowData = (Map<String,Object>)jsobj.get("protoData");
				
				
				//格式化时间
				String insertTimeStr = (String)rowData.get("inserttime");
				String updateTimeStr = (String)rowData.get("updatetime");
				
				insertTimeStr = DateUtils.getStrTime(insertTimeStr);
				updateTimeStr = DateUtils.getStrTime(updateTimeStr);
				
				rowData.put("inserttime", insertTimeStr);
				rowData.put("updatetime", updateTimeStr);
				
				List<String> hasSubDataList = new ArrayList<>();
				try {
					String subFieldName = (String)rowData.get("table_dest");
					if(subFieldName != null && !"".equals(subFieldName)) {
						if(subFieldName.contains(",")) {
							String[] subArr = subFieldName.split(subFieldName);
							for(String subStr : subArr) {
								if(subStr != null && !"".equals(subArr)) {
									hasSubDataList.add(subStr);
								}
							}
						}else {
							hasSubDataList.add(subFieldName);
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				
				try {
					for(String subStr : hasSubDataList) {
						String value = (String)rowData.get(subStr);
						String tableName = param.getString("protoName");
						String parentId = (Integer)rowData.get("id") + "";
						
						if(value == null || "".equals(value)) {
							value = "子集数据";
						}
						
						String setValue = "<a onclick=\"getSecondQuery('"+tableName+"','"+subStr+"','"+parentId+"')\">"+value+"<a/>";//"<a onclick=\"getSecondQuery('a','b','c')\">value_<a/>"+i);
						rowData.put(subStr, setValue);
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				resultList.add(rowData);
			}
			
			SYApiResult syResult = jsonObj.toJavaObject(SYApiResult.class);
			total = syResult.getData().getTotal();
			//syaList = syResult.getData().getContent();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
//		Map<String,Object> aaa = new HashMap<>();
//		for(int i=1 ; i<80; i++) {			
//			aaa.put("field_+"+i, "<a onclick=\"getSecondQuery('a','b','c')\">value_<a/>"+i);
//		}
//		resultList.add(aaa);
//		
//		Map<String,Object> bbb = new HashMap<>();
//		for(int i=1 ; i<80; i++) {			
//			bbb.put("field_+"+i, "value_+"+i);
//		}
//		resultList.add(bbb);
		
		//return getDataTableForApiServer(syaList,total);
		return getDataTableForApiServer(resultList,total);
    }
    
    /**
     * 业务数据管理维护-数据库数据管理 获取数据列表  二级页面
     */
    @GetMapping("/secondQueryPage")
    public String secondQueryPage(ModelMap mmap,HttpServletRequest request,String tableName, String fieldName, String parentId)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	//System.out.println("pppddddd:"+tableName+"=="+fieldName+"=="+parentId);
   	 	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
   	 	mmap.put("tableName", tableName);
   	 	mmap.put("fieldName", fieldName);
   	 	mmap.put("parentId", parentId);
        return "data_service/second_query_page";
    }
    
    
    /**
     * 业务数据管理维护-数据库数据管理 获取数据列表  二级查询
     */
    @PostMapping("/databaseListSecond")
    @ResponseBody
    public TableDataInfo databaseListSecond(HttpServletRequest request,String parentId,String tableName,String fieldName) {
    	//System.out.println("pppddddd:"+tableName+"=="+fieldName+"=="+parentId);
    	
    	
    	//parentId = "123";
    	long id = Long.parseLong(parentId);
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
    	JSONObject param = new JSONObject();
		param.put("id", id);//
		param.put("tableName", tableName);
		param.put("fieldName", fieldName);
		
		startPage();
    	PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
		param.put("pageSize", pageSize);//
		param.put("pageNo", pageNum-1);//
		param.put("tokenKey", TOKEN_KEY);
		
		List<Map> resultList = new ArrayList<Map>();
		
		//List<SYApiDataChild> syaList = new ArrayList<SYApiDataChild>();
		long total = 0;
		try {
			String result = PrometheusAPIUtils.payloadRequest("http://"+manageIp+":8081/protoData/querySecond", param);
//			System.out.println("二级查询结果:"+result);
			JSONObject jsonObj = JSONObject.parseObject(result);
			List<JSONObject> protocolsList = (List<JSONObject>)jsonObj.getJSONObject("data").get("content");
			
			
			for(JSONObject jsobj : protocolsList) {
				Map<String,Object> rowData = (Map<String,Object>)jsobj.get("protoData");
				resultList.add(rowData);
				for(String rd : rowData.keySet()) {
					System.out.println(rd+"==="+rowData.get(rd));
				}
			}
			
			SYApiResult syResult = jsonObj.toJavaObject(SYApiResult.class);
			total = syResult.getData().getTotal();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
//		Map<String,Object> aaa = new HashMap<>();
//		for(int i=1 ; i<80; i++) {			
//			aaa.put("field_+"+i, "<a onclick=\"getSecondQuery('a','b','c')\">value_<a/>"+i);
//		}
//		resultList.add(aaa);
//		
//		Map<String,Object> bbb = new HashMap<>();
//		for(int i=1 ; i<80; i++) {			
//			bbb.put("field_+"+i, "value_+"+i);
//		}
//		resultList.add(bbb);
		
		return getDataTableForApiServer(resultList,total);
    }
    
    /**
     * 业务数据管理维护-数据表配置
     */
    @GetMapping("/ordinaryFileDataManagement")
    public String ordinaryFileDataManagement(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/ordinary_file_data_management";
    }
    
    /**
     * 业务数据管理维护-数据库表管理
     */
    @GetMapping("/dataBaseTableConfig")
    public String dataBaseTableConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/database_table_config";
    }
    
    /**
     * 业务数据管理维护-视频回放配置
     */
    @GetMapping("/dataplaybackConfig")
    public String dataplaybackConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/data_playback_config";
    }
    
    /**
     * 业务数据管理维护-视频回放
     */
    @GetMapping("/dataplayback")
    public String dataplayback(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/data_playback";
    }
    
    /**
     * 业务数据管理维护-数据库备份管理配置
     */
    @GetMapping("/databasebak")
    public String databasebak(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/database_bak";
    }
    
    /**
     * 业务数据管理维护-类型配置
     */
    @GetMapping("/filetype")
    public String datafiletype(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/filetype_config";
    }
    
    /**
     * 业务数据管理维护-文件存储规则配置
     */
    @GetMapping("/storageRulesConfig")
    public String storageRulesConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/storage_rules_config";
    }
    
    /**
     * 业务数据管理维护-开放接口配置
     */
    @GetMapping("/openApiConfig")
    public String openApiConfig(ModelMap mmap,HttpServletRequest request)
    {
    	//获取管理分机IP
   	 	//String manageIp = configService.selectConfigByKey("manage_ip");
    	String manageIp = request.getServerName();
   	 	mmap.put("manageIp", manageIp);
        return "data_service/open_api";
    }
    
}
